package com.jixu.numberapp.utils;

import com.alibaba.fastjson.JSONObject;
import com.jixu.numberapp.server.WebSocketServer;
import com.jixu.numberapp.service.NumberService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Title:
 * Description:
 * Create Time: 2023/2/21 16:28
 *
 * @version 1.0
 * @author: aaa
 */
@Component
@Async
public class GameRule {
    public static String number = NumberUtils.getNumber(32); // 猜测的数字
    public static boolean flag = true;

    public static void guessNumber(NumberService service, String memberName, ThreadPoolTaskExecutor executor) throws IOException {
//        String number = NumberUtils.getNumber(32); // 猜测的数字
        List<Integer> integerList = new ArrayList<>(); //猜测位数集合
        // 定时器 2000毫秒一轮游戏
        Timer timer = new Timer(true);
        TimerTask task = new TimerTask() {
            public void run() {
                flag = true;
            }
        };
        timer.schedule(task, 3000l);
        for (int i = 0; i < number.length(); i++) {
            integerList.add(i); // 向集合里添加猜测位数
        }
        int end = 31; // 数字范围最大值
        int tureCount = 0;//猜对位数
        int count = 0;
//        boolean flag = true;// 判断条件
        int d = 0;
        while (flag) {
            count++;
            try {
                Thread.sleep(200l);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            int index = NumberUtils.getFiguresNumber(0, end); // 集合随机索引
            int n1 = integerList.get(index);// 拿到集合中随机位置的数
            Integer guessNumber = NumberUtils.getGuessNumber(); // 猜测的数字
            int i2 = Integer.parseInt(java.lang.String.valueOf(number.charAt(n1)));//母数随机索引位置的数
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("memberName", memberName);
            jsonObject.put("numberIndex", n1);
            jsonObject.put("time", LocalDateUtils.getLocalDateTimeStr());
            jsonObject.put("number", guessNumber);
            jsonObject.put("numberStr", number);
            jsonObject.put("flag", 0);
            jsonObject.put("messageType", 1);
            WebSocketServer.sendAll(jsonObject.toJSONString());
            JSONObject jsb = new JSONObject();
            jsb.put("count", count);
            jsb.put("messageType", 6);
            jsb.put("trueCount", tureCount);
            jsb.put("durationMin", d);
            if (count % 5 == 0) {
                d++;
                jsb.put("durationMin", d);
            }
            if (WebSocketServer.sendInfo(jsb.toJSONString(), memberName) == 1) {
                break;
            }
            // 判断
            if (guessNumber.compareTo(i2) == 0) {
                integerList.remove((Object) n1);// 去除不需要再猜的位数
                end--; // 猜测的范围-1 --> 0-31  > 0-30
                jsonObject.put("flag", 1);
                jsb.put("trueCount", n1);
                tureCount = n1;
                WebSocketServer.sendAll(jsonObject.toJSONString());
                number = NumberUtils.getNumber(32); // 刷新猜测的数字
                if (integerList.size() == 0) {
                    jsonObject.put("messageType", 2);
                    WebSocketServer.sendAll(jsonObject.toJSONString());
                    flag = false;
                }
            }
        }

    }
}


